package com.tcm.kafka;

import kafka.cluster.Partition;
import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.PartitionInfo;
import org.apache.kafka.common.TopicPartition;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

public class ConsumerPartition {

    public static final String brokeList = "localhost:9092";
    public static final String topic = "topic-demo";
    public static final String groupId = "group.demo";

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        properties.put("bootstrap.servers", brokeList);
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false);
        // 设置消费组的名称
        properties.put("group.id", groupId);
        //创建一个消费者客户端实例
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(properties);
        //订阅主题
//        consumer.subscribe(Collections.singletonList(topic));
        List<PartitionInfo> partitionInfos = consumer.partitionsFor(topic, Duration.ofMillis(1000));
        List<TopicPartition> list = new ArrayList();
        for(PartitionInfo partitionInfo : partitionInfos){
            TopicPartition partition = new TopicPartition(partitionInfo.topic(), partitionInfo.partition());
            list.add(partition);
        }
        consumer.assign(list);

//        批量提交
//        while(true){
//            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
//            for(ConsumerRecord record : records) {
//                System.out.println(record.value());
//            }
//            if(records.isEmpty()) break;
//            consumer.commitSync();
//        }

        //单条消息消费提交位移
//        while(true){
//            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
//            for(ConsumerRecord record : records) {
//                System.out.println(record.value());
//                long offset = record.offset();
//                TopicPartition partition = new TopicPartition(record.topic(), record.partition());
//                consumer.commitSync(Collections.singletonMap(partition, new OffsetAndMetadata(offset+1)));
//            }
//            if(records.isEmpty()) break;
//        }

        //根据分区消费消息
        while(true){
            ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
            for(TopicPartition partition:records.partitions()){
                List<ConsumerRecord<String, String>> partitionRecords = records.records(partition);

                for(ConsumerRecord<String,String> record:partitionRecords) {
                    System.out.println(record.value());
                }

                long lastConsumedOffset = partitionRecords.get(partitionRecords.size() - 1).offset();
                consumer.commitSync(Collections.singletonMap(partition, new OffsetAndMetadata(lastConsumedOffset+1)));
            }
            for(ConsumerRecord record : records) {
                System.out.println(record.value());
                long offset = record.offset();
                TopicPartition partition = new TopicPartition(record.topic(), record.partition());
                consumer.commitSync(Collections.singletonMap(partition, new OffsetAndMetadata(offset+1)));
            }
            if(records.isEmpty()) break;
        }
    }

}
